﻿using System;

namespace JuCheap.Core.Infrastructure.Extentions
{
    /// <summary>
    /// 时间扩展
    /// </summary>
    public  static class DateTimeExtentions
    {
        /// <summary>
        /// 时间转换成友好显示格式的字符串
        /// </summary>
        /// <param name="date">时间</param>
        /// <returns></returns>
        public static string ToFriendString(this DateTime date)
        {
            var now = DateTime.Now;
            var span = now.Subtract(date); 
            if (span.TotalSeconds <= 60)
            {
                return "刚刚";
            }
            else if (span.TotalMinutes <= 30)
            {
                return $"{(int)span.TotalMinutes}分钟前";
            }
            else if (span.TotalMinutes <= 60)
            {
                return $"1小时前";
            }
            else if (span.TotalHours <= 24)
            {
                return $"{(int)span.TotalHours}小时前";
            }
            else if (span.TotalDays <= 30)
            {
                return $"{(int)span.TotalDays}天前";
            }
            else if (span.TotalDays <= 365)
            {
                return $"{((int)span.TotalDays) / 30}个月前";
            }
            return $"{((int)span.TotalDays) / 365}年前";
        }
    }
}
